package com.wanqing.community.manager.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.wanqing.community.bean.UserDetailBean;
import com.wanqing.community.dao.UserDetailDao;
import com.wanqing.community.manager.UserDetailManager;
import com.wanqing.community.util.CommunityException;
import com.wanqing.community.util.ConditionParse;

/**
 * 用户明细业务逻辑实现
 * @author Jason
 *
 */
@Service("userDetailManager")
public class UserDetailManagerImpl implements UserDetailManager{

	private static Logger log = Logger.getLogger(UserDetailManagerImpl.class);
	
	@Resource
	private UserDetailDao userDetailDao;
	
	/**
	 *<p>Description:添加用户明细<p>
	 * <p>modifiContent:</p>
	 * @param list
	 * @return boolean
	 */
	public boolean addUserDetail(List<UserDetailBean> userDetail) throws CommunityException{
		int total = 0;
		try {
			for (UserDetailBean ud : userDetail) {
			total =  userDetailDao.addUserDetail(ud);	
			}
		}
		catch (CommunityException e) {
			log.error("添加用户明细时出现异常信息!"+e.toString());
			throw e;
		}
		return total>0?true:false;
	}
	
	
	/**
	  * <p>Description:根据主表ID查找用户明细<p>
	  * <p>modifiContent:<p>
	  * @param id
	  * @return
	  * @throws CommunityException
	  */
	public List<UserDetailBean> findUserDetailById(String ID) throws CommunityException 
	{		
		List<UserDetailBean> list = null;
		try{
			list = this.userDetailDao.findUserDetailById(ID);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}
	
	/**
	 * 修改用户明细
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param flowState
	 * @return
	 * @throws CommunityException
	 */
	public boolean updateUserDetail(List<UserDetailBean> userDetail) throws CommunityException{
		int i = 0;
		try{
			int total = 0;
			ConditionParse condParse = new ConditionParse();
			for (UserDetailBean ud : userDetail) {
				condParse.deleteAll();
				String udId = ud.getUdId();
				condParse.addCondition("udId", "=", udId);
				condParse.addWhereStr();
				total = this.userDetailDao.getUserDetailCount(condParse);
				if(total==0){
					i = this.userDetailDao.addUserDetail(ud);
				}else{
					i = this.userDetailDao.updateUserDetail(ud);
				}
			}
			
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return (i>0)?true:false;
	}
	
	/**
	  * <p>Description:根据ID删除用户明细<p>
	  * <p>modifiContent:<p>
	  * @param id
	  * @return
	  * @throws CommunityException
	  */
	public boolean deleteUserDetailbyId(String pcdId) throws CommunityException 
	{		
		int total=0;
		try 
		{
			total = this.userDetailDao.deleteUserDetailbyId(pcdId);
		} 
		catch (CommunityException e) 
		{
			log.error("根据ID删除明细异常！"+e.toString());
			throw e;
		}
		return (total>0)?true:false;
	}
	
}
